import { Message } from "element-ui";
import { deepTree, revDeepTree, isArray, isEmpty } from "cl-admin/utils";
import { revisePath } from "../utils";
import router from "@/router";
import { menuList } from "@/config/env";
import store from "store";

export default {
	state: {
		// 视图路由，type=1
		routes: store.get("viewRoutes") || [],
		// 树形菜单
		group: store.get("menuGroup") || [],
		// showAMenu 模式下，顶级菜单的序号
		index: 0,
		// 左侧菜单
		menu: [],
		// 权限列表
		permission: []
	},
	getters: {
		// 树形菜单列表
		menuGroup: state => state.group,
		// 左侧菜单
		menuList: state => state.menu,
		// showAMenu 模式下，顶级菜单的序号
		menuIndex: state => state.index,
		// 视图路由
		routes: state => state.routes,
		// 权限列表
		permission: state => state.permission
	},
	actions: {
		// 设置菜单、权限
		permMenu({ commit, state, getters }) {
			return new Promise((resolve, reject) => {
				const next = res => {
					res.menus = res.list;
					// res.menus.push({
					// 	end_type: 0,
					// 	ico: "menu-wxk",
					// 	is_permission: 1,
					// 	level: 0,
					// 	menu_desc: "",
					// 	menu_id: 450,
					// 	menu_name: "企业微信",
					// 	menu_url: "/",
					// 	parent_id: 0,
					// 	sort: 13,
					// 	spacer: "",
					// 	status: 0,
					// 	type: 1,
					// 	childlist: [{
					// 		childlist: [],
					// 		end_type: 0,
					// 		ico: "icon",
					// 		is_permission: 1,
					// 		level: 1,
					// 		menu_desc: "",
					// 		menu_id: 451,
					// 		menu_name: "账号配置",
					// 		menu_url: "/home/workwechat/setting",
					// 		parent_id: 450,
					// 		sort: 1,
					// 		spacer: "&nbsp;└",
					// 		status: 0,
					// 		type: 1
					// 	}]
					// })
					if (!isArray(res.menus)) {
						res.menus = [];
					}

					if (!isArray(res.perms)) {
						res.perms = [];
					}
					// 判断是否是移动端

					const formatArr = (list = []) => {
						let d = [];
						let id = 0;

						const deep = (list, parentId) => {
							list.forEach(e => {
								let r = {
									moduleName:
										e.moduleName ||
										(e.router || "").substr(1).replace(/\//g, "-"),
									id: e.menu_id,
									parentId: e.parent_id,
									path: revisePath(e.menu_url || e.id),
									viewPath: e.viewPath,
									// type: e.type,
									type: e.childlist.length > 0 ? 0 : 1,
									name: e.menu_name,
									icon: e.ico || "icon-favor",
									orderNum: e.sort,
									isShow: isEmpty(e.isShow) ? true : e.isShow,
									isMobile: e.is_mobile,
									meta: {
										label: e.menu_name,
										keepAlive: e.keepAlive
									},
									children: []
								};

								// 匹配存储的模块视图
								// let m = getters.moduleViews.find((m) => m.moduleName === r.moduleName);

								// if (m) {
								// 	// 注册组件实例
								// 	r.component = m.component;
								// }

								d.push(r);

								if (e.childlist && isArray(e.childlist)) {
									deep(e.childlist, e.menu_id);
								}
							});
						};

						deep(list || [], null);

						return d;
					};

					// const routes = res.menus
					// 	.filter(e => e.type != 2 && e.isShow)
					// 	.map(e => {
					// 		return {
					// 			moduleName: e.moduleName,
					// 			id: e.id,
					// 			parentId: e.parentId,
					// 			path: revisePath(e.router || e.id),
					// 			viewPath: e.viewPath,
					// 			type: e.type,
					// 			name: e.name,
					// 			icon: e.icon,
					// 			orderNum: e.orderNum,
					// 			isShow: isEmpty(e.isShow) ? true : e.isShow,
					// 			meta: {
					// 				label: e.name,
					// 				keepAlive: e.keepAlive
					// 			},
					// 			children: []
					// 		};
					// 	});

					// 转成树形菜单
					const menuGroup = formatArr(res.menus);
					let menuGroup1 = deepTree(menuGroup);
					menuGroup1.forEach(e => {
						if (e.isMobile) {
							e.isShow = false;
						}
					});
					// 设置权限
					commit("SET_PERMIESSION", res.perms);
					// 设置菜单组
					commit("SET_MENU_GROUP", menuGroup1);
					// 设置视图路由
					commit("SET_VIEW_ROUTES", menuGroup);
					// 设置菜单
					commit("SET_MENU_LIST", state.index);

					resolve(menuGroup);
				};

				// 监测自定义菜单
				if (!getters.app.conf.customMenu) {
					this.$service.common
						.permMenu()
						.then(res => {
							next(res);
						})
						.catch(err => {
							Message.error("菜单加载异常");
							console.error(err);
							reject(err);
						});
				} else {
					next({
						menus: revDeepTree(menuList)
					});
				}
			});
		}
	},
	mutations: {
		// 设置树形菜单列表
		SET_MENU_GROUP(state, list) {
			state.group = list;
			store.set("menuGroup", list);
		},

		// 设置视图路由
		SET_VIEW_ROUTES(state, list) {
			router.$plugin.addViews(list);

			state.routes = list;
			store.set("viewRoutes", list);
		},

		// 设置左侧菜单
		SET_MENU_LIST(state, index) {
			const { showAMenu } = this?.getters?.app?.conf || {};

			if (isEmpty(index)) {
				index = state.index;
			}

			if (showAMenu) {
				const { children = [] } = state.group[index] || {};

				state.index = index;
				state.menu = children;
			} else {
				state.menu = state.group;
			}
		},

		// 设置权限
		SET_PERMIESSION(state, list) {
			state.permission = list;
			store.set("permission", list);
		}
	}
};
